import { _decorator, Component, Node, Camera, size, UITransform, Canvas, Vec3 } from 'cc';
import GlobalData from '../../Init/Config/GlobalData';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';
import { GlobalTmpData } from '../../Init/Config/GlobalTmpData';
import EventManager from '../../Init/Managers/EventManager';
import { EventTypes } from '../../Init/Managers/EventTypes';
import GlobalPool from '../../Init/Tools/GlobalPool';
const { ccclass, property } = _decorator;

@ccclass('HPLayer')
export class HPLayer extends Component {
    hpRecs: { [uuid: number]: Node } = {};

    cameraUI: Camera = null;
    isRun = false;
    onLoad() {
        this.cameraUI = GlobalData.get(GlobalEnum.GlobalDataType.Camera3D);
        EventManager.on(EventTypes.GameEvents.GameRun, this.onGameRun, this);
        EventManager.on(EventTypes.EnemyEvents.showArmyBulletCd, this.onShowArmyBulletCd, this);
    }

    onEnable() {
        this.reset();
        this.setData();
    }

    reset() {
        this.hpRecs = {};
        this.isRun = false;
        GlobalPool.putAllChildren(this.node);
    }
    setData() {

    }

    update(dt) {
        if (!GlobalTmpData.Game.isGameRun || GlobalTmpData.Game.isGuideLv && !GlobalTmpData.Game.isGuideFinish) return;
        const _data = GlobalTmpData.HpData;
        //创建
        for (const uuid in _data) {
            const info = _data[uuid];
            if (info.rate == 0) {
                delete this.hpRecs[uuid];
            } else if (!this.hpRecs[uuid]) {
                let barName = 'hpBar';
                switch (info.type) {
                    case GlobalEnum.HpBarType.Army:
                        barName = 'armyHpBar';
                        break;
                    case GlobalEnum.HpBarType.Tank:
                        barName = 'tankHpBar';
                        break;

                    default:
                        break;
                }
                let e = GlobalPool.get(barName, { data: info, camera: this.cameraUI, parent: this.node });
                this.hpRecs[uuid] = e;
            }
        }
    }

    onGameRun() {
        this.isRun = true;
    }
    //显示敌方切换子弹cd
    onShowArmyBulletCd(d: { uuid: string, p: Vec3, cd: number }) {
        GlobalPool.get('armyBulletCd', { parent: this.node, camera: this.cameraUI, cd: d.cd, p: d.p })
    }
}

